This chapter explains how to use the CodeWarrior IDE facilities to search and replace text in files.
The topics in this chapter are:
The Find window, shown in Figure 6.1 on page 167 (Windows) and Figure 6.4 on page 173 (Mac OS and Solaris), is a versatile feature of the CodeWarrior IDE. To show the Find dialog box, choose the Find command in the Search Menu. Use this dialog box to perform find and replace operations for text in a single file or for text in multiple files in your project. You can use text strings, text substrings, and pattern matching to carry out find and replace operations.
There are two different sections in the Find dialog box.
This section presents a short tour of the find and replace user interface items in the Find window shown in Figure 6.1 on page 167. The items in the window are:
The Find text box is one of the editable text fields in the Find dialog box, shown in Figure 6.1. You enter text in this field that you want to search for.
You can use the Cut, Copy, Paste, and Clear commands with the Find text box. These commands are documented in the section called "Edit Menu" on page 603.
Also, the discussion "Enter `Find' String" on page 608 tells how to enter text into the Find Text Box without using the Find dialog box.
The Replace text box is one of the editable text fields in the Find dialog box, shown in Figure 6.1. The text you enter in this field will be used to replace the text you're searching for.
You can use the Cut, Copy, Paste, and Clear commands with the Find Text Box. These commands are documented in the section called "Edit Menu" on page 603.
Also, the discussion "Enter `Replace' String" on page 608 tells how to enter text into the Replace Text Box without using the Find dialog box.
The Recent Strings pop-up menu is shown in Figure 6.2. It contains strings that were recently used for searches.
There are actually two of these pop-ups. Each pop-up is to the right of both the Find text box and the Replace text box. Selecting an item in one of these pop-ups enters it in the corresponding text box.
The Find button, shown in Figure 6.1 on page 167, confirms the contents of the Find dialog box and begins the search. The settings in the dialog box are remembered and are displayed when the Find command is invoked again. The Find button is not available until you complete certain required text fields.
To learn more about finding text, see "Searching for Selected Text" on page 176.
The Replace button is one of the buttons in the Find dialog box, shown in Figure 6.1 on page 167.
When you enter text in the Find text box and click the Find
button, the CodeWarrior IDE will search for matching text according to the control settings checked at the bottom of the Find dialog box. If a match is found for the text in the Find text box, the Replace
button can be clicked to replace the found text with that shown in the Replace text box.
To learn more about searching and replacing text, see "Replacing Found Text" on page 182.
The Replace & Find button is shown in Figure 6.1 on page 167. This button behaves much like the Replace button, but also initiates another Find operation after the text substitution is performed.
To learn more about searching and replacing text, see "Finding and Replacing Text in a Single File" on page 178 and "Finding and Replacing Text in Multiple Files" on page 185.
The Replace All button is shown in Figure 6.1 on page 167. This button behaves much like the Replace button, but replaces every occurrence of the text shown in the Find text box with the text shown in the Replace text box.
To learn more about searching and replacing text, see "Replacing Found Text" on page 182.
The Batch checkbox is shown in Figure 6.1 on page 167. Selecting this checkbox causes the results of the Find
command to appear in a Search Results message window (Figure 6.7 on page 185).
To learn more about the role of the Batch checkbox in searching, see "Using Batch Searches" on page 184.
The Wrap checkbox is shown in Figure 6.1 on page 167. Selecting this checkbox allows a search that reaches one end of a file or group of files to continue from the opposite end.
To learn more about this feature, consult "Controlling Search Range in a Single File" on page 180.
The Ignore Case
checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to disregard the case (uppercase or lowercase) of the text entered into the Find text box.
To learn more about this feature, consult "Controlling Search Parameters" on page 181.
The Entire Word
checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to ignore matching text that occurs within words.
To learn more about this feature, consult "Controlling Search Parameters" on page 181.
The Regexp
checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to interpret the text in the Find text box as a regular expression.
CodeWarrior's regular expressions are similar to the regular expression for grep in UNIX. To learn more about this feature, refer to "Using Regular Expressions (grep)" on page 195.
The Multi-File Search Disclosure triangle is shown in Figure 6.1 on page 167. Click this triangle to show the Multi-File Search Section of the Find window, as shown in Figure 6.3 on page 171 (Windows) or Figure 6.4 on page 173 (Mac OS and Solaris).
To learn more about multi-file searching with the Find window, see "Finding and Replacing Text in Multiple Files" on page 185.
The Multi-File Search button is shown in Figure 6.3. Click this button to enable or disable the options in the Multi-File Search Section of the Find window, shown in Figure 6.4 on page 173.
When the Multi-File Search button is not depressed, as shown in the dialog box of Figure 6.3, the items in the Multi-File Search Section of the Find window are dimmed.
To learn more about the Multi-File Search Button, see "Activating Multi-File Search" on page 186.
This section describes the user interface items in the Multi-File Search Section of the Find window, shown in Figure 6.4. The user interface items are:
The File list is shown in Figure 6.4. This is a list of the files that will be searched in a Multi-file search. You add files to this list by enabling the Sources, System Headers, Project Headers, and Others controls. You can also drag and drop groups or files from the Project window into the list.
For more information about adding files and removing files in file sets, see "Choosing Files to be Searched" on page 187.
The File Sets pop-up menu is shown in Figure 6.5. This pop-up menu is used with Multi-file searches. Use this pop-up menu to select, add, and remove saved sets of files to search and replace.
For more information about Multi-file sets, see "Choosing Files to be Searched" on page 187.
The Project pop-up menu, shown in Figure 6.6, allows you to choose the project file with which you want to perform your search. Since the CodeWarrior IDE can have multiple projects open at a time, this menu provides a way to perform the same search in different projects.
If you turn off the Stop at End of File
checkbox, all the files in the File list are searched as one large file. When the CodeWarrior IDE reaches the end of one file, it starts searching the next. When it reaches the end of the last file to search, it beeps.
To search each file individually, enable the Stop at End of File checkbox. When the CodeWarrior IDE reaches the end of a file, it stops searching and beeps. You must choose Find in Next File from the Search Menu to continue the search.
For more information about using the Stop at End of File checkbox, see "Controlling Search Range in a Single File" on page 180.
The Sources checkbox is shown in Figure 6.4 on page 173. This checkbox adds all the source files from the current project to the File list.
For more information about source files in file sets, and their role in Multi-file searches, see "Adding project source files" on page 187.
The System Headers
checkbox is shown in Figure 6.4 on page 173. This checkbox adds all system header or interface files from the current project to the File list.
For more information about system headers in file sets, and their role in multi-file searches, see "Adding system header files" on page 188.
The Project Headers
checkbox is shown in Figure 6.4 on page 173. This checkbox adds all the project header or interface files from the current project to the File list.
For more information about project headers in file sets, and their role in Multi-file searches, see "Adding project header files" on page 188.
The Others
button is shown in Figure 6.4 on page 173. This button and its checkbox allows you to add one or many additional files to the File list.
For more information about adding file to file sets, see "Adding and removing arbitrary files" on page 188.
When you search for selected text, the CodeWarrior IDE uses the option settings that you last chose in the Find window. To change these option settings, you must use the Find dialog box.
You should know how to select text in the editor window before reading this section. If you don't know how to select text, refer to "Selecting Text" on page 148.
First, select an instance of the text you want to find. After selecting your text, choose Find Selection from the Search Menu.
The CodeWarrior IDE looks for the next occurrence of your text string in the current file only.
To search toward the end of the file for the next occurrence of the text string, click the Find button or choose Find Next from the Search Menu. You can also press the keyboard shortcut shown to the right of the Find Next command in the Search menu.
To search toward the beginning of the file for the previous occurrence of the text string, use the Find Previous command:
This command is available by pressing the key binding Shift-F3.
This command is available when you hold down the Shift key while using the Search Menu.
Search for more occurrences of the text string by continuing to use Find, Find Next, or Find Previous on the Search Menu.
First, select an instance of the text you want to find. After selecting your text, choose Enter `Find' String from the Search Menu. The editor enters the text in the Find text box of the Find dialog box.
Now make the window you want to search active, Then, choose Find Next from the Search Menu to search forwards in the active editor window for the next occurrence of your text string.
Use the Find Previous command to search backwards in the active editor window for the next occurrence of your text string:
This command is available by pressing the key binding Shift-F3.
This command is available when you hold down the Shift key while using the Search Menu.
The CodeWarrior IDE looks for the Find text box string in the active editor window, starting from the location of the text insertion point in that window.
If you want to search toward the end of the file for the next occurrence of the Find text box string, click the Find button or choose Find Next from the Search Menu. You can also press the keyboard shortcut shown to the right of the Find and Find Next commands in the Search menu.
To search toward the beginning of the file for the previous occurrence of the Find string, use the Find Previous command.
Search for more occurrences of the Find text box string by continuing to use Find, Find Next, or Find Previous from the Search Menu.
The Find window allows you to search for text patterns in the active editor window. When you find the text you are interested in, you can change it or look for another occurrence of it.
If you don't yet have a window open, see "Opening an Existing File" on page 108.
If you haven't yet created a file, see "Creating a New File" on page 107.
The topics in this section are:
To enter text in the Find text box, bring up the Find window using the Find command in the Search menu. Type a text string into the Find text box on the dialog box, or choose a string from the Recent Strings pop-up menu, as shown in Figure 6.2 on page 168.
Before searching, you can set other search options that control the range of your search.
The search range defines whether you want to search the entire file or just from the text insertion point in one direction. To set up the range of your search, see "Controlling Search Range in a Single File" on page 180.
The search parameters define whether you want to search for text regardless of text case, and whether to search partial words for the text. To set up the parameters of your search, see "Controlling Search Parameters" on page 181.
Before proceeding, make sure that multi-file searching is turned off since you are only interested in searching the active editor window. To learn about how to determine whether multi-file searching is turned off, refer to "Activating Multi-File Search" on page 186.
Click the Find button in the Find window to search forward from the text insertion point in the file, or choose Find or Find Next from the Search Menu. CodeWarrior now searches for the Find text box string in the active editor window.
Press the key binding Shift-F3 to perform a Find Previous command, which searches backwards from the text insertion point in the file.
Hold down the Shift key and choose Find Previous from the Search Menu if you want to search backwards from the text insertion point in the file.
To continue searching toward the end of the file for the next occurrence of the Find text box string, click the Find button or choose Find Next from the Search menu.
To continue searching toward the beginning of the file for the previous occurrence of the Find text box string, use the Find Previous command.
The editor finds and selects the Find text box string. If the string is not found, the editor beeps.
Search for more occurrences of the Find text box string by continuing to use Find, Find Next, or Find Previous.
From this point, you can replace some or all of the text you find with a new text string.
To replace text, see "Replacing Found Text" on page 182.
For example, suppose that you enable the Wrap checkbox option. When the text insertion point is somewhere in the middle of a source file in the active editor window, and you choose Find Next on the Search Menu, the CodeWarrior IDE searches from the insertion point to the end of the file. When the search reaches the end of the file, the search continues from the beginning of the file to the insertion point. In other words, the search "wraps" around the ends of the file.
The Find Previous command operates in a similar fashion. When the search reaches the beginning of the file and the Wrap checkbox option is enabled, the search continues from the end of the file.
If you have the Wrap checkbox option unchecked, and you choose Find Next on the Search Menu, the search stops when it reaches the end of the file.
If you're searching multiple files with the Wrap checkbox option checked, the CodeWarrior IDE searches from the first file in the file list after it reaches the last file.
The Ignore Case checkbox is shown in Figure 6.1 on page 167. When selected, the CodeWarrior IDE treats uppercase and lowercase text in the Find text box as identical. When deselected, uppercase and lowercase text are distinct.
For example, when you select the Ignore Case checkbox and enter Foobar
in the Find text box, the IDE finds occurrences of foobar
, FOOBAR
, and other possible combinations of uppercase and lowercase characters.
The Entire Word checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to find only complete words (delimited by punctuation or white-space characters) matching the text in the Find text box. If deselected, the IDE finds occurrences of the search text embedded within larger words. For example, suppose you enter Word
in the Find text box. If the Entire Word checkbox is selected, the IDE finds only the occurrences of Word
in the file. If the Entire Word checkbox is deselected, the IDE matches Word
even when it is embedded inside other text, such as Words
, WordCount
, and BigWordCount
.
To enter a Tab or Return character in the Find or Replace fields, use one of the following methods:
\t
for Tab or \r
for Return into the field.
Regexp
will alter the manner in which CodeWarrior locates a string match. See "Using Regular Expressions (grep)" on page 195 for more information on using Regexp.
To selectively replace text, first enter some text to find, then choose the Find operation on the Search Menu, or click the Find button in the Find dialog box. You can read more about how to find text by referring to "Finding Search Text" on page 178.
Next, enter the replacement text string in the Replace text box field of the Find dialog box.
Type the string in the Replace text box field or choose a string from the Recent Strings pop-up menu of the Replace text box by clicking the arrow icon just to the right. The Recent Strings pop-up menu (Figure 6.2 on page 168) contains the last five strings you have used.
To replace the string and see the results, click the Replace button in the Find dialog box or choose Replace from the Search Menu. The editor replaces the text that was found with the Replace text box string.
To continue searching forward, choose Find Next from the Search Menu, or click the Find button in the Find dialog box.
To continue searching backward, use the Find Previous command, or press the Shift key and click the Find button in the Find dialog box.
To replace the string and find the next occurrence, choose Replace & Find Next from the Search Menu, or click the Replace & Find button in the Find dialog box. The editor replaces the selected text with the Replace text box string and finds the next occurrence of the Find text box string. If the editor can't find another occurrence, it beeps.
To replace the Find text box string and find the previous occurrence, use the Replace & Find Previous command:
Press the key binding Ctrl-Shift-L, or press the Shift key as you click the Replace & Find button in the Find dialog box.
hold down the Shift key as you choose Replace & Find Previous from the Search Menu, or press the Shift key as you click the Replace & Find button in the Find dialog box.
The editor replaces the selected text with the Find text box string and searches for a previous occurrence of the Find text box string. If the CodeWarrior IDE can't find another occurrence, it beeps.
To replace text, first enter some text to find in the Find text box, then choose the Find operation on the Search Menu, or click the Find button in the Find dialog box. You can read more about how to find text by referring to "Finding Search Text" on page 178.
Next, enter the replacement text string in the Replace text box field of the Find dialog box.
To replace all the occurrences of the Find text box string, click the Replace All button in the Find dialog box, or choose Replace All from the Search Menu.
Replace All
command, since Undo
is not available for this operation.
Replace All
operation on a single source file, make sure to save the source file before executing the replace operation. In the event that you should change your mind, and before you save any changes, use Revert
to replace the modified file in memory with the saved version on disk. This technique will not work across multiple files.
If the Batch checkbox option is checked in the Find dialog box, and the Find button is clicked, the CodeWarrior IDE searches for all occurrences of the Find text box string and lists them in the Search Results message window, as shown in Figure 6.7.
The Search Results window shown in Figure 6.7 has a List View and a Source View.
To go to a particular occurrence of the Find text box string, so that it is shown in the Source View pane of the window, double-click on its entry in the List View.
To learn more about the features of this window, refer to the discussion of the Message Window in "Guided Tour of the Message Window" on page 373.
The CodeWarrior IDE allows you to search multiple files for the occurrence of text strings.
In this section you will learn how to do text searches through multiple files.
Another way to quickly access information and search in multiple files is with the Browser's Go Back and Go Forward commands on the Search menu. To learn about how to use these commands, refer to "Go Back and Go Forward" on page 242.
The topics in this section are:
When the Multi-File Search button is on, the button appears to be depressed.
When the Multi-File Search button is off, the button looks three-dimensional.
Click the Multi-File Search Disclosure triangle to the left of the Multi-File Search button, shown in Figure 6.1 on page 167, so that the triangle points down.
The CodeWarrior IDE displays the Multi-File Search Section, an extension of the Find dialog box, as shown in Figure 6.8 on page 187. When the Multi-File Search button is on (as shown above), the Multi-File Search Section is enabled.
To learn about how to configure the Multi-File Search Section of the Find dialog box, refer to "Choosing Files to be Searched" on page 187, "Saving a File Set" on page 191, "Removing a File Set" on page 192, and "Controlling Search Range in Multiple Files" on page 193.
There are several ways to choose files for a search.
To add all the source files from the current project, turn on the Sources checkbox. When you turn off the Sources checkbox, the CodeWarrior IDE removes all associated files from the file list.
To include only some of the files, turn on the Sources checkbox and delete the files you don't want by selecting them and pressing Backspace/Delete after clicking on the file name.
If turning on this option doesn't add any files, update your project's internal list of header and interface files with the Make command. To learn how to do this, refer to "Making a Project" on page 360.
To add all the project header or interface files from the current project, turn on the Project Headers checkbox. When you turn off the Project Headers checkbox, the CodeWarrior IDE removes all associated files from the file list.
To include only some of the files, turn on the Project Headers checkbox and delete the files you don't want by selecting them and pressing Backspace/Delete.
If turning on this option doesn't add any files, update your project's internal list of header or interface files with the Make command. To learn how to do this, refer to "Making a Project" on page 360.
To add all the system header or interface files from the current project, turn on the System Headers checkbox. When you turn off the System Headers checkbox, the CodeWarrior IDE removes all associated files from the file list.
To include only some of the files, turn on the System Headers checkbox and delete the files you don't want by selecting them and pressing Delete.
If turning on this option doesn't add any files, update your project's internal list of header or interface files with the Make command. To learn how to do this, refer to "Making a Project" on page 360.
First, click the Others button in the Multi-File Search Section of the Find dialog box. The Select Files To Search dialog box will be displayed as shown in Figure 6.9 (Windows), Figure 6.11 (Mac OS), and Figure 6.12 (Solaris).
You can select multiple files in this list by pressing the Control key and clicking a file simultaneously. When you're finished choosing files, click Add. All files in the Select Files To Search list will appear in the file set. To remove files from the file set, select them in the Find dialog box (Figure 6.8 on page 187) and press Backspace or Delete.
No
to return to the Select Files To Search dialog box. Instead of selecting the file's icon, select the file's name (including the quote marks) in the File Name text box and press Backspace or Delete. This is the correct way to remove a selected file from the Select Files To Search dialog box.
To add more files later, just click the Others button in the Find dialog box and repeat the process.
To select a previously-saved file set to include in your search, click on the File Sets pop-up menu and choose a file set from the menu, as shown in Figure 6.5 on page 174. The files then appear in the File Sets list.
To save a file set for use in future multi-file searches, choose Save this File Set
from the File Sets pop-up menu. The IDE displays the Save File Set dialog box shown in Figure 6.13 on page 193.
Name the file set by entering a name in the Save File Set As text field.
To remove a previously-saved file set, choose Remove a file set from the File Sets pop-up menu in the Find dialog box. The CodeWarrior IDE displays the dialog box shown in Figure 6.14.
You can choose to stop searching at the end of each file or to search all files without stopping.
To treat all the files in the file set as one large file, turn off the Stop at End of File checkbox. When the editor reaches the end of one file, it starts searching the next file until the selected text is found. The editor beeps when it reaches the end of the last file to search. After text is found, you may resume searching for the next occurrence using the Find, Find Next, or Find Previous menu commands.
To search each file individually, enable the Stop at End of File checkbox. The editor beeps when it reaches the end of a file. The arrow to the left of the file set indicates the file the editor is currently searching.
You must choose Find in Next File from the Search Menu or use the Find in Previous File command to continue the search. To start the search from a particular file, just select the file and click in the column to its left.
The Find in Previous File command is available by pressing the key binding Ctrl-Shift-T.
The Find in Previous File command is available by pressing the Shift key while using the Search Menu.
After choosing your option, proceed just as you would if you were searching only one file.
Find in Previous File
command. This effectively allows you to go backwards in your search into previously-searched files.
To learn more about text searching, see "Searching for Selected Text" on page 176, or "Finding and Replacing Text in Multiple Files" on page 185.
Regexp
checkbox is selected in the Find dialog box.
This section consists of the following topics:
The following characters have special meanings based upon their placement in the regular expression. For more information, see "Using Regular Expressions" on page 197.
MetaCharacters
|
Description
|
---|---|
#include <iostream>
#define var1 10;
#define var2 20;
using namespace std;
int result = 0;
int main(void)
{
cout << "This example provides information about" << endl;
cout << "the use of regular expressions in the" << endl;
cout << "CodeWarrior IDE." << endl << endl;
cout << "Refer to this code when reading the" << endl;
cout << "sections in the manual dealing with" << endl;
cout << "the use of regular expressions." << endl << endl;
cout << "The value of var1 is " << var1;
cout << " out of 50, and" << endl;
cout << "the value of var2 is " << var2;
cout << " out of 50." << endl;
cout << "$" << var1;
cout << " + $" << var2;
cout << " = $" <<;
result += var1;
result += var2;
cout << result;
cout << endl << endl;
return 0;
}
For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find every occurrence of the letter "m
" in the code, you would type m
into the Find text box. The CodeWarrior IDE will match this regular expression with the "m
" in iostream
. Additional searches will match with the "m
" in namespace
, main(void)
, example
, information
, and manual
.
If you want to find every occurrence of a dollar sign in the code, you would type \$
in the Find text box. The backslash tells the IDE to interpret the dollar sign as a normal character rather than a special character. The IDE will match the regular expression with the first dollar sign in the following line from Listing 6.1:
cout << "$" << var1;
Additional searches will match with the second and third dollar signs in the next two lines.
For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find four-character expressions in the code that begin with var
, type var.
in the Find text box. The period following the var
tells the IDE to look through the code for var
immediately followed by a single character. The IDE will match this regular expression with var1
and var2
in the code.
s*ion
in the Find text box, The s*
tells the IDE to match zero or more occurrences of the letter s
just before the occurrence of ion
. In Listing 6.1, this regular expression will match with the ion
in information
and sections
from the following lines:
cout << "This example provides information about" << endl;
cout << "sections in the manual dealing with" << endl;
The same regular expression will also match with the ssion
in expressions
from the following lines:
cout << "the use of regular expressions in the" << endl;
cout << "the use of regular expressions." << endl << endl;
If you want to find matches that have at least one letter s
preceding ion
, you would type s+ion
in the Find text box. The plus sign tells the IDE to match at least one occurrence of the letter s
just before the occurrence of ion
. This regular expression will match with the ssion
in expressions
from Listing 6.1. Notice that s+ion
does not match with the ion
in information
or sections
, since there is not at least one letter s
preceding ion
in either case.
If you want to find expressions in the code that contain the number zero, followed by one period or no periods at all, you would type 0\.?
in the Find text box. The backslash tells the IDE to treat the period as a normal character, and the ?
special operator acts on the normal period character. This expression will match with each occurrence of the number zero in Listing 6.1. This regular expression will also match with the 0.
from the following line:
cout << " out of 50." << endl;
As shown in these examples, the asterisk and plus sign usually refer to a single character. However, it is possible to refer to more than one character at a time by grouping expressions. See the following section, Grouping expressions, for more information.
For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find expressions in the code that match is
, you could simply type is
in the Find text box. However, you could also use ( i)s
as the regular expression. Notice that ( i)s
tells the IDE to look for the letter s
preceded by both a space and the letter i
. Whereas is
matches the is
within This
, this
, and is
, ( i)s
will match only with is
in the following two lines from Listing 6.1:
cout << "The value of var1 is " << var1;
cout << "The value of var2 is " << var2;
For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find expressions in the code that contain the letters x, y, or z, you would type [xyz]
in the Find text box. The IDE will match this regular expression with the letter x in example
and expressions
. If instead you want to find expressions in the code that do not contain the letters x, y, or z, type [^xyz]
in the Find text box. The IDE will match this regular expression with every character in the example code except the letter x in example
and expressions
.
You can specify that a regular expression match only the beginning or end of the line.
^
) is at the beginning of the entire regular expression, it matches the beginning of a line.
$
) is at the end of the entire regular expression, it matches the end of a line.
^like this$
), it matches an entire line.
cout
and occur only at the beginning of a line, you would type ^([ \t]*cout)
in the Find text box. The [ \t]*
in the regular expression allows zero or more spaces and tabs to precede cout
, as is the case with the code shown in Listing 6.1.
You can include the contents of the Find string in the Replace string by using an ampersand (&
) in the Replace string. For example, refer to the example code shown in Listing 6.1 on page 197. Suppose the Find string is var[0-9]
and the Replace string is my_&
. The editor will match the Find string with var1
and var2
in the code. Clicking the Replace button in the Find dialog box will replace var1
with my_var1
or var2
with my_var2
.
For example, refer to the example code shown in Listing 6.1 on page 197. If you want to change #define
declarations into const
declarations, you could perform a search for #define
and manually change the line of code to a const
declaration. However, you can take advantage of remembered sub-expressions to perform the same process automatically. Begin by typing the following regular expression in the Find text box:
\#define[ \t]+(.+)[ \t]+([0-9]+);
const int \1 = \2;
The \1
refers to the first sub-expression and the \2 refers to the second sub-expression from the Find string. These two sub-expressions recall the variable name and its value from the original #define
declaration. Notice that the replace string changes the #define
declaration into a const
declaration by using references to the two sub-expressions. Thus, the editor will find #define var1 10;
and change it into const var1 = 10;
in the code shown in Listing 6.1. It will change the next #define
statement in the same manner.